package com.zhuxin.events;

import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxConsts;
import com.soecode.wxtools.api.WxMessageHandler;
import com.soecode.wxtools.bean.*;
import com.soecode.wxtools.exception.WxErrorException;
import com.zhuxin.common.UtilTools;
import com.zhuxin.medical.mvc.controller.WxMsg;
import org.apache.commons.lang3.ArrayUtils;

import javax.transaction.Transactional;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lzb on 2018/11/13.
 */
@Transactional
public class SalerMsgHandler extends BaseHandler implements WxMessageHandler {


    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, IService iService) throws WxErrorException {
        WxXmlOutMessage xmlOutMsg = null;
        String rsmsg="";
        if(UtilTools.isNullOrBlank(WxMsg.curruser.get().getUsername())){
            String usercode=wxMessage.getContent();
            List<Map<String, Object>> users = this.oracleDao.queryForList("select USERCODE,USERNAME from sdc.sa_db_user where usercode=? and ISDISABELD=0",usercode);
            if(!users.isEmpty()){
                Integer ct=this.mysqlDao.queryForObject("select count(*) from commWxUser where usercode=?",Integer.class,usercode);
                if(ct==null || ct.intValue()==0) {
                    rsmsg = "您好，" + users.get(0).get("USERNAME") + ".你已经注册成功，可以使用下面菜单功能咯.";
                    WxMsg.curruser.get().setUsercode(users.get(0).get("USERCODE").toString());
                    WxMsg.curruser.get().setUsername(users.get(0).get("USERNAME").toString());
                    this.mysqlDao.updateSub(WxMsg.curruser.get(), ArrayUtils.toArray("USERCODE", "USERNAME"), true);
                }else{
                    rsmsg="账号已被注册，请联系管理员.";
                }
            }else{
                rsmsg="账号无效,首次使用请输入销售系统用户名.";
            }
        }else {
            Object o=this.checkUserStatus(wxMessage,context,iService);
            if(o instanceof String)rsmsg= (String) o;
            else xmlOutMsg= (WxXmlOutMessage) o;
        }
        if(xmlOutMsg==null) {
            KfSender sender = new KfSender();
            sender.setTouser(wxMessage.getFromUserName());
            sender.setMsgtype(WxConsts.MASS_MSG_TEXT);
            sender.setText(new SenderContent.Text(rsmsg));
            sender.setCustomservice(new SenderContent.CustomService("kf2001@linyukeji007"));
            iService.sendMessageByKf(sender);
        }
//        if(xmlOutMsg==null)xmlOutMsg = WxXmlOutMessage.TEXT().toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();
        return xmlOutMsg;
    }


    private List<Long> matchPlanids(String ctx){
        List<Long> pids=new ArrayList<>(20);
        Pattern pt=Pattern.compile("[0-9]+");
        Matcher matcher = pt.matcher(ctx);
        while(matcher.find()){
            pids.add(Long.valueOf(matcher.group()));
        }
        return pids;
    }
}
